package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.model.PersonDB;
import com.captainbank.tcl.tclapplication.model.SkillDB;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.GreendaoUtil;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.google.gson.Gson;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/10/15.
 * Describe:
 */
public class GreendaoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_id)
    EditText etId;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idcard)
    EditText etIdcard;
    @BindView(R.id.et_age)
    EditText etAge;
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;
    @BindView(R.id.tv_show)
    TextView tvShow;

    private boolean isMan = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_greendao;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_greendao));

    }

    @Override
    protected void setListener() {
        rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_woman:
                        isMan = false;
                        break;

                    case R.id.rb_man:
                    default:
                        isMan = true;
                        break;
                }
            }
        });
    }

    @OnClick({R.id.btn_add, R.id.btn_del, R.id.btn_update, R.id.btn_query, R.id.btn_query_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add:
                String idStrAdd = etId.getText().toString().trim();
                String nameStrAdd = etName.getText().toString().trim();
                String ageStrAdd = etAge.getText().toString().trim();
                String idCardStrAdd = etIdcard.getText().toString().trim();
                PersonDB personDBAdd = getPersonDB(idStrAdd, nameStrAdd, ageStrAdd, idCardStrAdd);
                if (null != personDBAdd) {
                    int type = new Random().nextInt(10)+1;
                    SkillDB skillDB = new SkillDB();
                    skillDB.setId(Long.parseLong(idStrAdd));
                    skillDB.setSkillName("技能：" + type);
                    skillDB.setSkillDesc("技能描述：" + type);
                    GreendaoUtil.addSkill(skillDB);

//                    SkillDB skillDBNew = GreendaoUtil.querySkill();
                    personDBAdd.setSkillId(skillDB.getId());
                    personDBAdd.setSkillDB(skillDB);
                    GreendaoUtil.addPerson(personDBAdd);
                }
                break;
            case R.id.btn_del:
                String idStrDel = etId.getText().toString().trim();
                if (StringUtil.checkIsNotNull(idStrDel)) {
                    long id = Long.parseLong(idStrDel);
                    GreendaoUtil.delPerson(id);
                } else {
                    MyToast.toastNormal(this, "请输入ID");
                }
                break;
            case R.id.btn_update:
                String idStrUpdate = etId.getText().toString().trim();
                String nameStrUpdate = etName.getText().toString().trim();
                String ageStrUpdate = etAge.getText().toString().trim();
                String idCardStrUpdate = etIdcard.getText().toString().trim();
                PersonDB personDBUpdate = getPersonDB(idStrUpdate, nameStrUpdate, ageStrUpdate, idCardStrUpdate);
                if (null != personDBUpdate) {
                    GreendaoUtil.updatePerson(personDBUpdate);
                }
                break;
            case R.id.btn_query:
                String idStrQuery = etId.getText().toString().trim();
                if (StringUtil.checkIsNotNull(idStrQuery)) {
                    long id = Long.parseLong(idStrQuery);
                    List<PersonDB> list = GreendaoUtil.queryPersonById(id);
                    showData(list);
                } else {
                    MyToast.toastNormal(this, "请输入查询ID");
                }
                break;

            case R.id.btn_query_all:
                List<PersonDB> list = GreendaoUtil.queryPersonAll();
                showData(list);
                break;
        }
    }

    //获取用户信息
    private PersonDB getPersonDB(String idStr, String nameStr, String ageStr, String idcardStr) {
        if (!StringUtil.checkIsNotNull(idStr)) {
            MyToast.toastNormal(this, "请输入ID");
            return null;
        } else if (!StringUtil.checkIsNotNull(nameStr)) {
            MyToast.toastNormal(this, "请输入用户名");
            return null;
        } else if (!StringUtil.checkIsNotNull(idcardStr)) {
            MyToast.toastNormal(this, "请输入身份证号");
            return null;
        } else if (!StringUtil.checkIsNotNull(ageStr)) {
            MyToast.toastNormal(this, "请输入年龄");
            return null;
        } else {
            PersonDB personDB = new PersonDB();
            long id = Long.parseLong(idStr);
            personDB.setId(id);
            personDB.setName(nameStr);
            int age = Integer.parseInt(ageStr);
            personDB.setAge(age);
            long idCard = Long.parseLong(idcardStr);
            personDB.setInCard(idCard);
            personDB.setIsMan(isMan);
            return personDB;
        }
    }

    //显示数据
    private void showData(List<PersonDB> list) {
        String personStr = new Gson().toJson(list);
        tvShow.setText(personStr);
    }

}
